@include('admin.public.head')
@include('admin.public.pop')

<style>
    .right_content{
        float:right;
    }

    .c p,.c span{
        color: #0c9c6e;
    }

    .d p,.d span{
        color: blue;
    }

    .is_camp{
        width: 100px;
        height: 35px;
        background-color: #0c9c6e;
        line-height: 35px;
        color: white;
        border-radius: 5px;
        display: inline-block;
        font-size: 10px;
        text-align: center;
    }

    .no_is_camp{
        width: 100px;
        height: 35px;
        background-color: blue;
        line-height: 35px;
        color: white;
        border-radius: 5px;
        display: inline-block;
        font-size: 10px;
        text-align: center;
    }
</style>

<link href="{{ URL::asset('./admin/css/home.css') }}" rel="stylesheet">
<div class="ibox-content">
    <div class="is_camp">camp课预约</div>
    <div class="no_is_camp">单节课预约</div>

    <button class="btn btn-primary" onclick="up()" style="border:1px solid #337ab7 !important;">上一周</button>
    <button class="btn btn-primary" onclick="down()" style="border:1px solid #337ab7 !important;">下一周</button>

{{--    <a href="/class_base_info" class="btn btn-success">设置等级</a>--}}

    <div class="col-sm-1">
        <select class="form-control" name="user_rank_id" >


        </select>
    </div>

    <input type="hidden" id="start" value="0">
    <input type="hidden" id="end" value="7">

    @php date_default_timezone_set("Asia/Shanghai"); @endphp

    <input type="hidden" id="today" value="{{strtotime(date('Y-m-d'))*1000}}">

    <div class="btn-groups">
        <span class="men"></span>
    </div>

    <div class="mm-table">
        <table class="table table-bordered table-hover">

        </table>
    </div>
</div>

<script type="text/javascript">
    var time_global = '';
    var t = new Date();
    var open = '';
    var zhuan_id = 0;
    var _data = {}

    var _tplobj = [] // 转模板

    var w = ['日', '一', '二', '三', '四', '五', '六'];

    time_global = t.getFullYear() + '-' + (t.getMonth() + 1 >= 10 ? t.getMonth() + 1 : '0' + (t.getMonth() + 1)) + '-' + (t.getDate() >= 10 ? t.getDate() : '0' + (t.getDate()));

    week_global = '星期' + w[t.getDay()];

    $(function (){
        getDay();
        showSingle();
    });

    $("select[name='user_rank_id']").change(function (){
        showSingle($(".active"));
    });

    //修改等级
    function edit_rank(obj)
    {
        var _this = obj.parents('.edit_user_rank');
        var user_rank_id = new Array();
        var class_base_info_id = _this.attr('class_base_info_id');

            _this.find('input:checked').each(function(){
            user_rank_id.push($(this).val());
        });

        $.ajax({
            url: '/class_base_info_edit_level',
            data: {
                class_base_info_id: class_base_info_id,
                user_rank_id: user_rank_id
            },
            type: 'POST',
            success: function (res) {


            }
        })
    }

    function up(){
        var oneDay = 3600 * 24 * 1000*7;
        var today = $("#today").val();
        today = parseInt(today);
        today-=oneDay;
        $("#today").val(today)

        getDay();
        showSingle($(".active"));
    }

    function down(){
        var oneDay = 3600 * 24 * 1000*7;
        var today = $("#today").val();
        today = parseInt(today);
        today+=oneDay;
        $("#today").val(today)

        getDay();
        showSingle($(".active"));
    }

    function getDay (){
        var today = $("#today").val();

        today = parseInt(today);

        var data = calcData(today)

        render(data)
    }

    function calcData(first){
        var data = [];
        var oneDay = 3600 * 24 * 1000;

        var today = $("#today").val();
        today = parseInt(today);

        for(var i = 0; i < 7; i++){
            var calc = first + oneDay * i;
            data.push(calc);
        }

        return data;
    }

    function render (data){
        var parent = $('.men')
        var w = [];             //放假周期
        var d = [];             //单天放假
        //var w = eval("(" + '{$holiday_week_store|raw}' + ")")
        //console.log(w)
        //var d = eval("(" + '{$holiday_date|raw}' + ")")
        //console.log(d)
        parent.html('');
        data.forEach(function (cur, index){
            //console.log(cur)
            var date = new Date(cur);
            var week = ['日', '一', '二', '三', '四', '五', '六'];
            var color = ['#337ab7', '#337ab7', '#337ab7', '#337ab7', '#337ab7', '#337ab7', '#337ab7'];
            var curWeek = week[date.getDay()];
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var year = date.getFullYear();
            var c = color[date.getDay()];
            if(month < 10) month = '0' + month
            if(day < 10) day = '0' + day
            var isdisabled = ''

            if(w.length > 0){
                w.forEach(function(cur, index){
                    if(cur.week == `星期${curWeek}`){
                        isdisabled = 'disabled';
                        c = '#666'
                    }
                })
            }

            if(d.length > 0){
                d.forEach(function(cur, index){
                    if(cur.time == `${year}-${month}-${day}`){
                        isdisabled = 'disabled'
                        c = '#666'
                    }
                })
            }

            if(index == 0) {
                var tpl = `<button class="btn btn-primary active" ${isdisabled} onclick="showSingle(this)" style="border:1px solid ${c} !important; " data-week="星期${curWeek}" data-time="${year}-${month}-${day}">${month}月${day}日 星期${curWeek}</button>`
            }else{
                var tpl = `<button class="btn btn-primary" ${isdisabled} onclick="showSingle(this)" style="border:1px solid ${c} !important; " data-week="星期${curWeek}" data-time="${year}-${month}-${day}">${month}月${day}日 星期${curWeek}</button>`
            }

            parent.append(tpl)
        })
    }

    function showSingle(_this){

        var _that = !!_this ? $(_this) : '';

        var time = '';

        var user_rank_id = $("select[name='user_rank_id'] option:selected").val();

        if(!!_this){
            time = _that.data('time') || time_global;
            time_global = _that.data('time');
        }else{
            time = time_global;
        }

        if(_that){
            _that.addClass('active').siblings().removeClass('active');
            _that.css("background",'');
        }

        var loadding_index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });

        $.ajax({
            url: '/home',
            data: {
                day: time,
                user_rank_id: user_rank_id
            },
            type: 'POST',
            success: function (res) {

                user_rank(res);

                layer.close(loadding_index);

                _data = res.data;

                var _tpl = tpl(_data, time)

                _tplobj = _tpl

                changTpl (_tplobj.html1)

                layer.close(open)

            }
        })
    }

    function user_rank(res)
    {
        $("select[name='user_rank_id']").html(null);
        var user_rank = '<option value="0">會員等級</option>';

        for (var i=0;i<res.data.user_rank.length;i++){
            user_rank += '<option value="'+res.data.user_rank[i].id+'" ';

            if(res.data.user_rank_id == res.data.user_rank[i].id){
                user_rank += "selected";
            }

            user_rank += '>'+res.data.user_rank[i].rank_name+'</option>';
        }

        $("select[name='user_rank_id']").append(user_rank);
    }

    function changTpl (obj){
        $(".table").html(obj.join(''));
    }

    function tpl(res, time){

        var html = [];

        var user_rank = '';

        if(typeof(res.user_rank) != 'undefined')user_rank = res.user_rank;

        if(res.classroom_lists.length > 0){  // 课室

            res.classroom_lists.forEach(function (classroom_cur, classroom_index){

                var button = '';

                @if(permission('/class_base_info_add_home', $user['right_list'], $user['get_routes']))
                button += '<button url="/class_base_info_add_home/'+classroom_cur.id+'/'+time+'" onclick=\'toggle_home($(this),\"100%\",\"100%\",\"安排課程\")\' class="btn btn-primary right_content">安排课程</button>';
                @endif

                html.push('<tr><td><p><b>'+classroom_cur.numbering+'&nbsp;/&nbsp;'+classroom_cur.store.store_name+'&nbsp;&nbsp;'+button+'</b></p></td>');

                    var _h = ''

                    if(res.arr.length > 0){

                        res.arr.forEach(function(yy_cur){

                            if(yy_cur.class_classroom_id == classroom_cur.id) {

                                if(yy_cur.is_camp == 1){
                                    is_class="c";
                                }else{
                                    is_class="d";
                                }

                                _h += '<td class="'+is_class+'">';
                                @if(permission('/class_base_info_del', $user['right_list'], $user['get_routes']))
                                _h += '<a href="javascript:void(0)" onclick="del($(this))" url="/class_base_info_del/' + yy_cur.id +'" style="color:red">删除</a>';
                                @endif
                                @if(permission('/class_sign_code', $user['right_list'], $user['get_routes']))
                                _h += '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="toggle_info($(this),\'100%\',\'100%\')" url=\'/class_sign_code/' + yy_cur.id +'/'+time+'\' style="color:red">签到</a>';
                                @endif

                                @if(permission('/class_base_info_edit_home', $user['right_list'], $user['get_routes']))
                                _h += '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="toggle_info($(this),\'100%\',\'100%\')" url=\'/class_base_info_edit_home/'+classroom_cur.id+'/'+time+'/'+yy_cur.id +'\' style="color:red">編輯</a>';
                                @endif

                                @if(permission('/class_base_info_teacher', $user['right_list'], $user['get_routes']))
                                    _h += '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="toggle_info($(this),\'100%\',\'100%\')" url=\'/class_base_info_teacher/'+yy_cur.id+'\' style="color:red">上课老师</a>';
                                @endif

                                @if(permission('/home_index', $user['right_list'], $user['get_routes']))
                                _h += '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="toggle_info($(this),\'100%\',\'100%\')" url=\'/home_index/'+yy_cur.id+'/'+time+'/'+'\' style="color:red">查看</a>';
                                @endif

                                @if(permission('/home_reservation_lists', $user['right_list'], $user['get_routes']))
                                _h += '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="toggle_info($(this),\'100%\',\'100%\')" url=\'/home_reservation_lists/'+yy_cur.id+'/'+time+'/'+yy_cur.is_camp+'\' style="color:red">操作</a>';
                                @endif

                                _h += "<p><span class='sdfdf'>上课时间段：</span>" + yy_cur.class_time.time_start+'~'+yy_cur.class_time.time_end + "</p>";
                                _h += "<p><span class='sdfdf'>上课赛季：</span>" + yy_cur.quarterly.name+ "</p>";
                                _h += "<p><span class='sdfdf'>所屬門店：</span>" + yy_cur.store.store_name + "</p>";
                                _h += "<p><span class='sdfdf'>教室編號：</span>" + yy_cur.classsroom.numbering + "</p>";
                                _h += "<p><span class='sdfdf'>老師名稱：</span>" + yy_cur.teacher.teacher_name + "</p>";
                                _h += "<p><span class='sdfdf'>周期：</span>" + yy_cur.class_week + "</p>";
                                _h += "<p><span class='sdfdf'>課程名：</span>" + yy_cur.class_base.class_name + "</p>";
                                _h += "<p><span class='sdfdf'>可容納上課人數：</span>" + yy_cur.class_number_people + "</p>";
                                _h += "<p><span class='sdfdf'>已预约人数：</span>" + yy_cur.class_subscribe_number + "</p>";
                                _h += "<p><span class='sdfdf'>预约成功：</span>" + yy_cur.appointment_successful + "</p>";
                                _h += "<p><span class='sdfdf'>预约取消：</span>" + yy_cur.appointment_failed + "</p>";
                                _h += "<p><span class='sdfdf'>排隊中：</span>" + yy_cur.appointment_appointment + "</p>";
                                _h += "<p><span class='sdfdf'>是否开启预约：</span>" + yy_cur.is_display_name + "</p>";

                                var rank_name = '';



                                if(user_rank != '') {
                                    for (var i = 0; i < user_rank.length; i++) {
                                        rank_name += user_rank[i].rank_name + '&nbsp;<input onchange="edit_rank($(this))"  name="user_rank_id" type="checkbox" value="' + user_rank[i].id + '" ';

                                        if(yy_cur.user_rank_id.indexOf(user_rank[i].id.toString()) >= 0){
                                            rank_name += 'checked="checked"';
                                        }

                                        rank_name += '>&nbsp;';
                                    }
                                }

                                _h += "<p class='edit_user_rank' class_base_info_id='"+yy_cur.id+"'><span class='sdfdf'>会员等级：</span>" + rank_name + "</p>";


                                _h += '</td>';
                            }
                        });

                    }

                    if(_h == ''){
                        _h = '<td></td>';
                    }

                    html.push(_h);

                html.push('</tr>');

            })

        }

        return {
            html1 : html,
        }

    }

    function cencal(id){
        layer.open({
            content : '确定取消预约?',
            title : '取消预约',
            btn : ['确定', '取消'],
            yes : function (index, layero){

            },
            btn2 : function (index, layero){
                layer.close(index)
            }

        })
    }

    function z(_this){
        var _that = $(_this);
        var id = zhuan_id
        var sid = _that.data('sid')
        var rid = _that.data('rid')

    }

    function change(id){
        zhuan_id = id;
        $('.close2').show()
        changTpl(_tplobj.html2)
    }

    function change2(){
        changTpl (_tplobj.html1)
        $('.close2').hide()
    }

    function toggle_home(obj, width, height, message = '') {
        var url = obj.attr('url');
        var jump_url = obj.attr('jump_url');

        //自定页
        parent.layer.open({
            type: 2,
            title: message,
            skin: 'layui-layer-rim', //加上边框
            area: [width, height], //宽高
            content: [url, 'yes'],
            end: function () {
                showSingle();
            }
        });
    }

    function del(obj){
        var url = obj.attr('url');
        var title =  "你確定要刪除? 請注意已預約的人，并且妥善處理";
        var botton =  "刪除";
        var result =  "刪除成功";

        swal({
            title: title,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: botton,
            closeOnConfirm: false
        }, function () {
            $.ajax({
                type: "post",
                url: url,
                dataType: "json",
                success: function(data) {

                    var swal_json = {
                        title : result,
                        type : "success"
                    }

                    swal(swal_json,function(isConfirm){
                        showSingle();
                    });

                }
            });

        });
    }

</script>
</body>
</html>

